package com.example.cc.server.config;

import com.example.cc.common.rabbitmq.RabbitConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServerRabbitConfig {

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(RabbitConstants.EXCHANGE_NAME, true, false);
    }

    public Queue clientQueue(String clientId) {
        return new Queue(RabbitConstants.CLIENT_QUEUE_PREFIX + clientId, true);
    }

    // 绑定队列到交换机
    public Binding bindQueueToExchange(Queue queue, String clientId) {
        String bindingKey = clientId + ".#";
        return BindingBuilder.bind(queue)
                .to(topicExchange())
                .with(bindingKey);
    }

}
